package com.yy.yyui.anime
{
	import flash.display.BitmapData;
	import flash.events.TimerEvent;
	import flash.utils.Timer;

	public class AnimeBitmapData extends BitmapData
	{
		private var timer:Timer;
		private var _timeFrames:Array;
		private var framePoint:int;
		private var updateFun:Function;
		public function AnimeBitmapData(timeFrames:Array,updateFunction:Function,width:int, height:int, transparent:Boolean=true, fillColor:uint=0)
		{
			//TODO: implement function
			super(width, height, transparent, fillColor);
			
			this._timeFrames = timeFrames;
			this.updateFun = updateFunction;
			
			framePoint = 0;
			timer = new Timer(this.timeFrames[0]);
			timer.addEventListener(TimerEvent.TIMER,onTime);
			this.startAnime();
		}
		
		private function startAnime():void{
			this.timer.start();
		}
		
		private function get timeFrames():Array{return this._timeFrames;}
		private function set timeFrames(value:Array):void{this._timeFrames = value;}
		
		private function get updateFunction():Function{return this.updateFun;}
		private function set updateFunction(value:Function):void{this.updateFun = value;}
		
		private function onTime(e:TimerEvent):void{
			framePoint++;
			if(framePoint>=this.timeFrames.length){
				framePoint=0;
			}
			this.updateFunction(this,framePoint);
			timer.delay = this.timeFrames[framePoint];
		}
		
		
	}
}